package com.hualan.servlet;

import com.alibaba.fastjson.JSON;
import com.hualan.bean.Shop;
import com.hualan.dao.MenDianDao;
import com.hualan.dao.MenDianDaoImpl;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.print.Book;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/update")
public class PanghuUpdateServlet extends HttpServlet {
//    get  用于通过id获取需要编辑的门店对象
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int shopid= Integer.parseInt(req.getParameter("shopId"));
        MenDianDao menDianDao= new MenDianDaoImpl();
        Shop shop = menDianDao.selectMenDianById(shopid);
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        Map<String,Object> map = new HashMap<>();
        if (shop == null){
            map.put("code",300);
            map.put("message","id不合法");
        }
        else {
            map.put("code",200);
            map.put("message","获取成功");
            map.put("data",shop);
//            map.put("typeList",typeList);
        }

        resp.getWriter().write(JSON.toJSONString(map));
    }
    //post 用于提交编辑信息到数据库
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        int shopId= Integer.parseInt(req.getParameter("shopId"));
        String shopName = req.getParameter("shopName");
        String shopAddress = req.getParameter("shopAddress");
        String shopCity = req.getParameter("shopCity");
        String shopDescribe = req.getParameter("shopDescribe");
        String shopDistrict = req.getParameter("shopDistrict");
        String shopImage = req.getParameter("shopImage");
        String shopLatitude = req.getParameter("shopLatitude");
        String shopLongitude = req.getParameter("shopLongitude");
        String shopPhone = req.getParameter("shopPhone");
        String shopProvince = req.getParameter("shopProvince");
        String addTime = req.getParameter("addTime");
        MenDianDao menDianDao = new MenDianDaoImpl();
        Shop shop = new Shop(shopName,shopDescribe,shopImage,shopPhone,shopProvince,shopCity,
                shopDistrict,shopAddress,shopLongitude,shopLatitude,addTime,shopId);
        int row = menDianDao.updateMenDian(shop);
        Map<String,Object> map = new HashMap<>();
        if (row >0 ){
            map.put("code",200);
            map.put("message","成功");
        }else {
            map.put("code",300);
            map.put("message","失败");
        }
        resp.getWriter().write(JSON.toJSONString(map));


    }
}
